package com.example.bms.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.bms.entity.ApiResult;
import com.example.bms.entity.Score;
import com.example.bms.serviceimpl.ScoreServiceImpl;
import com.example.bms.util.ApiResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;


/**
 * @apiNote 查询学生成绩
 * @author
 * @email 1627856348@qq.com
 * @since 1.0.0
 */
@RestController
@CrossOrigin(origins = "*", methods = {RequestMethod.GET, RequestMethod.POST,RequestMethod.PUT,RequestMethod.DELETE})
public class ScoreController {
    @Autowired
    private ScoreServiceImpl scoreService;


    /**
     * @apiNote 查询所有学生成绩
     * @author
     * @email 1627856348@qq.com
     * @return ApiResult
     * @since 1.0.0
     */
    @GetMapping("/scores")
    public ApiResult findAll() {
        List<Score> res = scoreService.findAll();
        return ApiResultHandler.buildApiResult(200,"查询所有学生成绩",res);
    }


    /**
     * @apiNote 根据ID查询成绩
     * @param page 试卷
     * @param size 数量
     * @param studentId 学生id
     * @author
     * @email 1627856348@qq.com
     * @return ApiResult
     * @since 1.0.0
     */
//    分页
    @GetMapping("/score/{page}/{size}/{studentId}")
    public ApiResult findById(@PathVariable("page") Integer page, @PathVariable("size") Integer size, @PathVariable("studentId") Integer studentId) {
        Page<Score> scorePage = new Page<>(page, size);
        IPage<Score> res = scoreService.findById(scorePage, studentId);
        return ApiResultHandler.buildApiResult(200, "根据ID查询成绩", res);
    }


    /**
     * @apiNote 根据ID查询成绩
     * @param studentId 学生id
     * @author
     * @email 1627856348@qq.com
     * @return ApiResult
     * @since 1.0.0
     */
    @GetMapping("/score/{studentId}")
        public ApiResult findById(@PathVariable("studentId") Integer studentId) {
        List<Score> res = scoreService.findById(studentId);
        if (!res.isEmpty()) {
            return ApiResultHandler.buildApiResult(200, "根据ID查询成绩", res);
        } else {
            return ApiResultHandler.buildApiResult(400, "ID不存在", res);
        }
    }


    /**
     * @apiNote 根据ID查询成绩
     * @param score 分数实体类
     * @author
     * @email 1627856348@qq.com
     * @return ApiResult
     * @since 1.0.0
     */
    @PostMapping("/score")
    public ApiResult add(@RequestBody Score score) {
        int res = scoreService.add(score);
        if (res == 0) {
            return ApiResultHandler.buildApiResult(400,"成绩添加失败",res);
        }else {
            return ApiResultHandler.buildApiResult(200,"成绩添加成功",res);
        }
    }


    /**
     * @apiNote 根据examCode（考试编号）查询成绩
     * @param examCode 考试编号
     * @author
     * @email 1627856348@qq.com
     * @return ApiResult
     * @since 1.0.0
     */
    @GetMapping("/scores/{examCode}")
    public ApiResult findByExamCode(@PathVariable("examCode") Integer examCode) {
        List<Score> scores = scoreService.findByExamCode(examCode);
        return ApiResultHandler.buildApiResult(200,"查询成功",scores);
    }
}
